package com.laulkar.activities;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.laulkar.Constants;
import com.laulkar.R;

public class SubwaySelector extends ListActivity{
	
	private SubwayIconAdapter m_adapter;
	private List<String> subway_status;
	SharedPreferences pref;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//Bundle extras = getIntent().getExtras();
		//subway_status = extras.getParcelableArrayList(SUBWAY_STATUS_KEY);
		setDisplay();
		//this.subway_status = Main.mta_status.subways;
		pref = getSharedPreferences(Constants.SHARED_PREF_SUBWAY, 0);

		this.m_adapter = new SubwayIconAdapter(this, R.layout.subway_image_list, subway_status);
		setListAdapter(this.m_adapter);
	}
	
	public void setDisplay()
	{
			
			subway_status = new ArrayList<String>();
			
			for(String s : Constants.subway_icons.keySet() )
			{
				subway_status.add(s);
			}
			Collections.sort(subway_status);
	}
	
	protected void onListItemClick(ListView l, View v, int position, long id) 
	{
		super.onListItemClick(l, v, position, id);
		String s;

		s = (String) getListView().getItemAtPosition(position);
		ImageView icon = (ImageView) v.findViewById(R.id.image_icon_row);
		
		if(icon != null)
		{
			if(isSubwayLineSelected(s))
			{
				icon.setAlpha(100);
				deSelectSubway(s);
				
			}
			else
			{
				icon.setAlpha(255);
				selectSubway(s);
			}
		}
	}
	
	public boolean isSubwayLineSelected(String line)
	{
		
		return pref.getBoolean(line, false);
	}
	
	public boolean selectSubway(String line)
	{
		Log.i("SSelector", "Line" + line + " Selected");
		Editor e = pref.edit();
		e.putBoolean(line, true);
		e.commit();
		return true;
	}
	
	public boolean deSelectSubway(String line)
	{
		Log.i("SSelector", "Line" + line + " De - Selected");
		Editor e = pref.edit();
		e.putBoolean(line, false);
		e.commit();
		return true;
	}
	
	private class SubwayIconAdapter extends ArrayAdapter<String> {

		private List<String> items;

		public SubwayIconAdapter(Context context, int textViewResourceId, List<String> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.subway_image_row, null);
			}
			String s = items.get(position);
			if (s != null) {
				
				ImageView icon = (ImageView) v.findViewById(R.id.image_icon_row);
				if(icon != null)
				{
					
						icon.setImageResource(Constants.subway_icons.get(s));
						if(! isSubwayLineSelected(s)){
							icon.setAlpha(100);
						}
					
				}
				
				//if(bt != null){
					//      bt.setText("Status: "+ o.getOrderStatus());
					//}
			}
			return v;
		}
	}
}
